﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework; // Vector2 e demais classes para posicionamento.
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;

namespace first_project
{
    class Player : Object2D
    {
        #region Atributes and Variables
        /// <summary>
        /// Amount of life the player has
        /// </summary>
        int lifes;

        // Movimentando
        animation stand;
        animation moveRight;
        animation jump;
        animation crouch;
        animation moveLeft;

        private List<Shot> shots;
        public List<Shot> Shots
        {
            get { return shots; }
      
        }

        

        #region Shot
        Texture2D shotImage;

        private Vector2 shotSpeed;
        #endregion

        GameWindow window;        

        private float lastTime;

        #endregion

        public Player(Texture2D image,Texture2D ShotImage,GameWindow Window) : base(image)
        {
            this.window = Window;
            this.shotImage = ShotImage;
            this.Position = new Vector2(50, 50);
            this.CollisionRect = new Rectangle(0, 0, 48, 50);
            

            stand = new animation();

            stand.frameCount = 5;
            stand.frameHeight = 50;
            stand.frameRate = 5;
            stand.frameWidth = 43;
            stand.mapX = 5;
            stand.mapY = 174;

            moveRight.frameCount = 14;
            moveRight.frameHeight = 56;
            moveRight.frameRate = 9;
            moveRight.frameWidth = 55;
            moveRight.mapX = 0;
            moveRight.mapY = 434;

            jump.frameCount = 3;
            jump.frameHeight = 38;
            jump.frameRate = 9;
            jump.frameWidth = 39;
            jump.mapX = 0;
            jump.mapY = 76;

            crouch.frameCount = 3;
            crouch.frameHeight = 38;
            crouch.frameRate = 9;
            crouch.frameWidth = 39;
            crouch.mapX = 0;
            crouch.mapY = 114;

            moveLeft.frameCount = 11;
            moveLeft.frameHeight = 38;
            moveLeft.frameRate = 9;
            moveLeft.frameWidth = 39;
            moveLeft.mapX = 0;
            moveLeft.mapY = 152;

            currentAnimation = stand;

            this.shotSpeed = new Vector2(5, 5);
            shots = new List<Shot>();            
            lastTime = 0;
        }

        public override void Update()
        {
            
        }

        public void Update(GameTime gameTime)
        {

            for (int k = 0; k < shots.Count; k++)
            {
                shots[k].Update(gameTime);

            }

            for (int j = 0; j < shots.Count; j++)
            {
                if (shots[j].isShotDeleted())
                {
                    shots.Remove(shots[j]);
                }
            }


            //Not Using right now
            #region Movimento do Player

            /*Position += Speed;

            if (Keyboard.GetState().IsKeyDown(Keys.Right))
            {
                position.X += 5;
            }
            else if (Keyboard.GetState().IsKeyDown(Keys.Left))
            {
                position.X -= 5;
            }

            if (Keyboard.GetState().IsKeyDown(Keys.Up))
            {
               Direction = new Vector2((float)Math.Cos(Rotation), (float)Math.Sin(Rotation));
                Speed += Direction * Acceleration;
            }

            if (Keyboard.GetState().IsKeyDown(Keys.Space))
            {
                

                if (gameTime.TotalGameTime.TotalMilliseconds >= lastTime+150)
                {
                    lastTime = (float)gameTime.TotalGameTime.TotalMilliseconds;
                    Vector2 shotPosition = new Vector2(position.X + Image.Width / 2 * (float)Math.Cos(Rotation), position.Y + Image.Height / 2 * (float)Math.Sin(Rotation));
                    Vector2 shotDirection = new Vector2((float)Math.Cos(Rotation), (float)Math.Sin(Rotation));
                    //Shot shot = new Shot(shotImage, window, shotSpeed, shotDirection, shotPosition);

                    //shots.Add(shot);
                    

                }

            }*/

            #endregion

            //Not using right now
            #region Rotação da nave

            /*if (Position.X >= window.ClientBounds.Width + Image.Width / 2)
            {
                position.X = -Image.Width / 2;
            }
            else if (Position.X <= -Image.Width / 2)
            {
                position.X = window.ClientBounds.Width + Image.Width / 2;
            }
            else if (Position.Y >= window.ClientBounds.Height + Image.Height / 2)
            {
                position.Y = -Image.Height / 2;
            }
            else if (Position.Y <= -Image.Height / 2)
            {

                position.Y = window.ClientBounds.Height + Image.Height / 2;
            }*/

            #endregion

           

        }


        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(Image, Position, null, Color.White, Rotation, new Vector2(Image.Width / 2, Image.Height / 2), 1, SpriteEffects.None, 0);

            foreach (Shot shot in shots)
            {

                shot.Draw(spriteBatch);
            }
        }

    }

}
